package com.jarvis.app.car.adapter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.jarvis.app.car.R;
import com.jarvis.app.car.model.Car;
import com.jarvis.app.car.model.User;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class HomeCarItemAdapter extends RecyclerView.Adapter<HomeCarItemAdapter.HomeCarItemAdapterHolder> {

    private List<Car> cars = new ArrayList<>();

    private ItemViewOnClickListener itemViewOnClickListener;

    private Activity parent;

    public void setItemViewOnClickListener(ItemViewOnClickListener itemViewOnClickListener) {
        this.itemViewOnClickListener = itemViewOnClickListener;
    }

    public List<Car> getCars() {
        return cars;
    }

    public void setCars(List<Car> cars) {
        this.cars = cars;

        //一定要刷新
        notifyDataSetChanged();
    }

    public HomeCarItemAdapter(Activity parent) {
        this.parent = parent;
    }

    public HomeCarItemAdapter() {
    }

    @NonNull
    @Override
    public HomeCarItemAdapterHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //加载布局
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.car_list_item, null);
        return new HomeCarItemAdapterHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull HomeCarItemAdapterHolder holder, int position) {
        //绑定数据
        Car car = getCars().get(position);

        Glide.with(parent)
                .load(car.getImg())
                .into(holder.iv_carImg);

        holder.tv_carName.setText(car.getName());
        holder.tv_carCode.setText(car.getCode());

        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (Objects.nonNull(itemViewOnClickListener)) {
                    itemViewOnClickListener.deleteOnLongClick(car, position);
                }
                return true;
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Objects.nonNull(itemViewOnClickListener)) {
                    itemViewOnClickListener.infoOnClick(car, position);
                }
            }
        });
    }

    private Bitmap returnBitMap(String path) {
        Bitmap bitmap = null;
        try {
            URL url = new URL(path);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.connect();

            InputStream inputStream = httpURLConnection.getInputStream();
            bitmap = BitmapFactory.decodeStream(inputStream);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return bitmap;
    }


    @Override
    public int getItemCount() {
        return getCars().size();
    }

    static class HomeCarItemAdapterHolder extends RecyclerView.ViewHolder {


        ImageView iv_carImg;

        TextView tv_carName;

        TextView tv_carCode;

        public HomeCarItemAdapterHolder(@NonNull View itemView) {
            super(itemView);
            iv_carImg = itemView.findViewById(R.id.iv_carImg);

            tv_carName = itemView.findViewById(R.id.tv_carName);

            tv_carCode = itemView.findViewById(R.id.tv_carCode);

        }
    }

    public interface ItemViewOnClickListener {
        void deleteOnLongClick(Car car, int position);


        void infoOnClick(Car car,int position);
    }

}
